clear 
matrix drop _all
use processed/RPImicro,clear
forvalues x = 1/17	 {
gen b`x' = s_RPI_`x'
drop if b`x' ==.
}
drop if I ==.
drop if I < 0
 
**quantile data*****
preserve
keep if percent != .
keep pp_value percent datayear
reshape wide pp_value, i(percent) j(datayear)
drop percent
outsheet using DataforMatlab\IP.csv, comma replace nonames
restore
keep b* datayear I weight caseno
outsheet using DataforMatlab\UK_Budget_Raw.csv, comma replace nonames
 
gen logI = log(I)
gen logI2 = log(I)*log(I)
gen logI3 = log(I)*log(I)*log(I)
keep if I > 0
qui forvalues x = 1/17	 {
keep if b`x' <1 
keep if b`x' > -0.00001 
}

*clear
*clear
set matsize 4000 

tempname b_cons
tempname b_logI
tempname b_logI2

matrix `b_cons' = J(44, 17, 0)
matrix `b_logI' = J(44, 17, 0)
matrix `b_logI2' = J(44, 17, 0)


tempname I_min
tempname I_max
tempname I_p5
tempname I_p25
tempname I_p50
tempname I_p75
tempname I_p95

matrix `I_min' = J(44, 1, 0)
matrix `I_max' = J(44, 1, 0)
matrix `I_p5' = J(44, 1, 0)
matrix `I_p25' = J(44, 1, 0)
matrix `I_p50' = J(44, 1, 0)
matrix `I_p75' = J(44, 1, 0)
matrix `I_p95' = J(44, 1, 0)


local row = 1



qui forvalues x = 1/17	 {
    reg b`x' logI logI2 if datayear == 1974, vce(robust)
    matrix `b_cons'[`row', `x'] = _b[_cons]
    matrix `b_logI'[`row', `x'] = _b[logI]
    matrix `b_logI2'[`row', `x'] = _b[logI2]
    predict tmp if e(sample), xb
    replace tmp = 0 if tmp < 0
    gen b`x'_hat = tmp
    drop tmp

    * Calculate I_min, I_max, and percentiles
        summarize I if datayear == 1974, detail
        matrix `I_min'[`row', 1] = r(min)
        matrix `I_max'[`row', 1] = r(max)
        matrix `I_p5'[`row', 1] = r(p5)
        matrix `I_p25'[`row', 1] = r(p25)
        matrix `I_p50'[`row', 1] = r(p50)
        matrix `I_p75'[`row', 1] = r(p75)
        matrix `I_p95'[`row', 1] = r(p95)
}
local row = `row' + 1

qui forvalues t = 1975/2017 {
    forvalues x = 1/17 {
        reg b`x' logI logI2 if datayear == `t', vce(robust)
        matrix `b_cons'[`row', `x'] = _b[_cons]
        matrix `b_logI'[`row', `x'] = _b[logI]
        matrix `b_logI2'[`row', `x'] = _b[logI2]

        predict tmp if e(sample), xb
        replace tmp = 0 if tmp < 0
        replace b`x'_hat = tmp if datayear == `t'
        drop tmp

        // Calculate I_min, I_max, and percentiles
        summarize I if datayear == `t', detail
        matrix `I_min'[`row', 1] = r(min)
        matrix `I_max'[`row', 1] = r(max)
        matrix `I_p5'[`row', 1] = r(p5)
        matrix `I_p25'[`row', 1] = r(p25)
        matrix `I_p50'[`row', 1] = r(p50)
        matrix `I_p75'[`row', 1] = r(p75)
        matrix `I_p95'[`row', 1] = r(p95)
    }
	local row = `row' + 1

}


// Display matrices
matrix list `b_cons'
matrix list `b_logI'
matrix list `b_logI2'


// Save all coefficients into one dataset
preserve
// Concatenate matrices
matrix combined_matrix = `b_cons', `b_logI', `b_logI2', `I_min', `I_max', `I_p5', `I_p25', `I_p50', `I_p75', `I_p95'

// Get number of rows and columns in the combined matrix
local rows = rowsof(combined_matrix)
local cols = colsof(combined_matrix)

// Convert the combined matrix to a dataset
clear
set obs `rows'

gen year = 1974
local colnum = 1
forvalues c = 1/`cols' {
    local varname
    if `c' <= 17 {
        local varname = "b_cons_`colnum'"
    }
    else if `c' <= 34 {
        local varname = "b_logI_`colnum'"
    }
    else if `c' <= 51 {
        local varname = "b_logI2_`colnum'"
    }
    else if `c' <= 52 {
        local varname = "I_min"
    }
    else if `c' <= 53 {
        local varname = "I_max"
    }
    else if `c' <= 54 {
        local varname = "I_p5"
    }
    else if `c' <= 55 {
        local varname = "I_p25"
    }
    else if `c' <= 56 {
        local varname = "I_p50"
    }
    else if `c' <= 57 {
        local varname = "I_p75"
    }
    else {
        local varname = "I_p95"
    }

    gen `varname' = combined_matrix[1, `c']
    forvalues r = 2/`rows' {
        replace `varname' = combined_matrix[`r', `c'] in `r'
    }

    if `colnum' == 17 {
        local colnum = 1
    }
    else {
        local colnum = `colnum' + 1
    }
}

replace year = year[_n-1] + 1 if _n > 1

// Export the dataset to a CSV file
outsheet using "DataforMatlab/engel_coefficients.csv", comma replace

restore


